#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode 
{
    int val;
    struct ListNode *next;
};

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
    struct ListNode* tail = NULL;
    struct ListNode* newhead = NULL;
    if (list1 == NULL && list2 == NULL)
    {
        return NULL;
    }
    else if(list1==NULL)
    {
        return list2;
    }
    else if (list2 == NULL)
    {
        return list1;
    }
    else
    {
        while (cur1 && cur2)
        {
            if (cur1->val < cur2->val)
            {
                if (newhead == NULL)
                {
                    tail = newhead = cur1;
                    cur1 = cur1->next;
                }
                else
                {
                    tail->next = cur1;
                    cur1 = cur1->next;
                    tail = tail->next;
                }
            }
            else
            {
                if (newhead == NULL)
                {
                    tail = newhead = cur2;
                    cur2 = cur2->next;
                }
                else
                {
                    tail->next = cur2;
                    cur2 = cur2->next;
                    tail = tail->next;
                }
            }
        }
    }
    if (cur1 == NULL)
    {
        tail->next = cur2;
    }
    else
tail->next = cur1;
    return newhead;
}

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k)
{
    struct ListNode* fast = pListHead;
    struct ListNode* slow = pListHead;
    if (pListHead == NULL)
    {
        return NULL;
    }
    while (k--)
    {
        if (fast == NULL)
        {
            return NULL;
        }
        fast = fast->next;
    }
    while (fast)
    {
        fast = fast->next;
        slow = slow->next;
    }
    return slow;
}

struct ListNode* middleNode(struct ListNode* head) 
{
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    while (fast&&fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
    }
    return slow;
}

//struct ListNode* reverseList(struct ListNode* head)
//{
//    struct ListNode* cur = head;
//    struct ListNode* prev = NULL;
//    if (cur==NULL)
//    {
//        return NULL;
//    }
//    while (cur)
//    {
//        struct ListNode* next = cur->next;
//        cur->next = prev;
//        prev = cur;
//        cur = next;
//    }
//    return prev;
//}

struct ListNode* reverseList(struct ListNode* head) 
{
    struct ListNode* cur = head;
    struct ListNode* newhead = NULL;
    while (cur)
    {
        struct ListNode* next = cur->next;
        cur->next = newhead;
        newhead = cur;
        cur = next;
    }
    return newhead;
}
 
struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* newhead = NULL,*tail=NULL;
    struct ListNode* cur =  head;
    while (cur)
    {
        if (cur->val != val)
        {
            if (tail == NULL)
            {
                newhead = tail = cur;
           }
            else
            {
                tail->next = cur;
                tail = tail->next;
            }
            cur = cur->next;
        }
        else
        {
            struct ListNode* next = cur->next;
            free(cur);
            cur = next;
        }
    }
    if (tail)
        tail->next = NULL;
    return newhead;
}
int main()
{
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->val = 1;
    n2->val = 2;
    n3->val = 3;
    n4->val = 4;

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = NULL;

    //removeElements(n1, 1);
    reverseList(n1);
    return 0;
}